package shanhu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Wptnew项目的上线拷贝class
 * @author DengZhiYuan
 * @version 1.0
 * @date 2023/3/7 11:17
 */
public class XinHuaFileCopy {

    public static void main(String[] args) {
        XinHuaFileCopy fileCopy = new XinHuaFileCopy();
        fileCopy.copyFiles();
    }

    /**
     * 获取这个文件以后的路径
     */
    private static final Date limitDate = DateUtil.parse("2022-11-25 19:40:01", DatePattern.NORM_DATETIME_PATTERN);

    /**
     * 拷贝需要上传到服务器的文件
     * @author DengZhiYuan
     * @version 1.0
     * @date 2023/3/7 14:56
     * @param 
     * @return 
     */
    private void copyFiles(){
//        String basePath = "/opt/web/tomcat7-8078-wpttest/webapps/wpt/WEB-INF/classes";
        String sourceBasePath = "D:\\IdeaProjects\\xinhua\\out\\artifacts\\xinhua_Web_exploded\\WEB-INF\\classes";
        String saveBasePath = "C:\\Users\\dzy\\Desktop\\禅道1423旧有备案接口改造\\xinhua\\classes";
        //先清空以前的目录
        FileUtil.clean(new File(saveBasePath));

        List<String> uploadFiles = this.getUploadFiles();
        for (String item :uploadFiles) {
            String actualFileName = StrUtil.replace(item, ".java", ".class");
            String filePath = sourceBasePath + actualFileName;
            String savePath = saveBasePath + actualFileName;
            FileUtil.copy(new File(filePath), new File(savePath), true);
        }
        System.out.println("拷贝文件完成");
    }



    /**
     * 每次上线，需要拷贝出修改的文件
     * @author DengZhiYuan
     * @version 1.0
     * @date 2023/3/7 11:20
     * @param 
     * @return 
     */
    private List<String> getUploadFiles(){
        String basePath = "D:\\IdeaProjects\\xinhua\\src";
        List<String> allUpdateFile = new ArrayList<>();
        File file = new File(basePath);
        TextFilesFilter filesFilter = new TextFilesFilter();
        for (File item : file.listFiles()) {
            readFiles(item,allUpdateFile,filesFilter);
        }
        List<String> allNewFile = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(allUpdateFile)){
            allUpdateFile.forEach(e->{
                String replace = StrUtil.replace(e, basePath, "");
                allNewFile.add(replace);
            });
        }

        allNewFile.forEach(e->System.out.println(e));
        return allNewFile;
    }

    private void readFiles(File f,List<String> allUpdateFile,FileFilter filter){
        if(f.isDirectory()){
            for (File  f2 : f.listFiles()) {
                this.readFiles(f2,allUpdateFile,filter);
            }
        }else{
            if(f.exists() && f.canRead() && filter != null && filter.accept(f)){
                allUpdateFile.add(f.getPath());
            }
        }
    }

    private static class TextFilesFilter implements FileFilter {
        @Override
        public boolean accept(File file) {
            long lastModified = file.lastModified();
            if(limitDate.compareTo(new Date(lastModified)) < 0){
                return true;
            }
            return false;
        }
    }

}
